﻿using IRepositoryBll;
using IRepositoryDal;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using RepositoryCommon;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    /// <summary>
    /// 用户业务访问层
    /// </summary>
    public class UserInfoBll : BaseDeleteBll<UserInfo>,IUserInfoBll
    {
        private IUserInfoDal _userInfoDal;
        private IDepartmentInfoDal _departmentInfoDal;


        public UserInfoBll(IUserInfoDal userInfoDal, IDepartmentInfoDal departmentInfoDal):base(userInfoDal)
        {
            _userInfoDal = userInfoDal;
            _departmentInfoDal = departmentInfoDal;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        //public bool AddUserInfo(UserInfo userInfo)
        //{
        //    UserInfoDal userInfoDal = new UserInfoDal();
        //    return _userInfoDal.Add(userInfo);
        //}

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        //public bool DeleteUserInfo(string userInfoId)
        //{
        //    return _userInfoDal.Delete(userInfoId);
        //}

        /// <summary>
        /// 根据id获取用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        //public UserInfo GetUserInfo(string userInfoId)
        //{
        //    return _userInfoDal.GetEntity(userInfoId);
        //}


        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetUserInfoList()
        {
            return _userInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false).ToList();
        }

        /// <summary>
        /// 获取用户集合（分页）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public object GetUserInfoListByPage(int page, int limit,out int count, string userName, string phoneNum)
        {
            //获取数据库中用户全部没删除的数据（未真实查询）
            var userInfos = _userInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);

            if (!string.IsNullOrEmpty(userName))
            {
                userInfos =  userInfos.Where(u => u.UserName.Contains(userName));
            }
            if (!string.IsNullOrEmpty(phoneNum))
            {
                userInfos = userInfos.Where(u => u.PhoneNum.Contains(phoneNum));
            }

            //查询出来数据的数量
            count = userInfos.Count();
            //获取部门数据集合
            DbSet<DepartmentInfo> departmentInfos = _departmentInfoDal.GetEntitiesDb();
            //连表查询
            var temp = from u in userInfos
                       join d in departmentInfos
                       on u.DepartmentID equals d.ID into udTemp
                       from ud in udTemp.DefaultIfEmpty()
                       select new
                       {
                           u.UserName,
                           u.ID,
                           u.Sex,
                           u.Account,
                           u.PhoneNum,
                           u.Email,
                           u.CreateTime,
                           ud.DepartmentName
                       };

            //分页
            temp = temp.Skip((page - 1) * limit).Take(limit);

            var list = temp.ToList().Select(u => 
            {
                string sexStr;
                if (u.Sex == 1)
                {
                    sexStr = "男";
                }
                else
                {
                    sexStr = "女";
                }

                return new
                {
                    u.UserName,
                    u.ID,
                    u.Account,
                    u.PhoneNum,
                    u.Email,
                    
                    u.DepartmentName,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Sex = sexStr
                };
            });

           

            return list;
        }

        /// <summary>
        /// 软删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        //public bool SoftDeleteUserInfo(string userInfoId)
        //{
        //    UserInfo userInfo = _userInfoDal.GetEntity(userInfoId);
        //    if (userInfo != null)
        //    {
        //        userInfo.IsDelete = true;
        //        userInfo.DeleteTime = DateTime.Now;
        //        return _userInfoDal.Update(userInfo);
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        
        //public bool UpdateUserInfo(UserInfo userInfo)
        //{
        //    //return _userInfoDal.UpdateUserInfo(userInfo);
            
        //    return _userInfoDal.Update(userInfo);
        //}

        /// <summary>
        /// 更新用户的用户名、姓名
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="userName"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(string userInfoId, string userName, int sex, string phoneNum ,string departmentInfoId)
        {
            UserInfo userInfo = _userInfoDal.GetEntity(userInfoId);
            if (userInfo != null)
            {
                userInfo.UserName = userName;
                userInfo.Sex = sex;
                userInfo.PhoneNum = phoneNum;
                userInfo.DepartmentID = departmentInfoId;
                return _userInfoDal.Update(userInfo);
            }
            else
            {
                return false;
            }

        }
        //登录
        
       
       
        public UserInfo GetUserInfoByLogin(string account, string passWord)
        {
      
            return _userInfoDal.GetEntitiesDb().Where(u => u.Account == account && u.PassWord == passWord && u.IsDelete == false).FirstOrDefault();

        }

        /// <summary>
        /// 获取用户名（领导）下拉选数据
        /// </summary>
        /// <returns></returns>
        public object GetSelectOption()
        {
            //linq
            var lerder = _userInfoDal.GetEntitiesDb().Where(d => !d.IsDelete).Select(d => new
            {
                d.ID,
                d.UserName
            }).ToList();
            return lerder;
        }

        public bool EditPassword(string oldPwdMD5, string newPwdMD5, UserInfo userInfo, out string msg)
        {
            throw new NotImplementedException();
        }
    }
}
